﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VehicleTransactionsViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the VehicleTransactionViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Windows.Forms;
    using Business.Interfaces;
    using Common;
    using Documents;
    using Hisaab.Common;

    using Microsoft.Practices.ServiceLocation;

    using Model;

    /// <summary>
    /// Defines the <seealso cref="VehicleTransactionsViewModel"/> type.
    /// </summary>
    public class VehicleTransactionsViewModel : ViewModelBase
    {
        /// <summary>
        /// The Save Command
        /// </summary>
        public static readonly ActiveAwareCommand EditCommand = new ActiveAwareCommand();

        /// <summary>
        /// The Delete Command
        /// </summary>
        public static readonly ActiveAwareCommand DeleteCommand = new ActiveAwareCommand();

        /// <summary>
        /// The refresh command
        /// </summary>
        public static readonly ActiveAwareCommand RefreshCommand = new ActiveAwareCommand();

        /// <summary>
        /// Account object
        /// </summary>
        private readonly IVehicleTransactionManager _vehicleTransactionManager;

        /// <summary>
        /// Selected vehicle transaction
        /// </summary>
        private VehicleTransaction _selectedVehicleTransaction;

        /// <summary>
        /// Vehicle transactions list
        /// </summary>
        private ObservableCollection<VehicleTransaction> _vehicleTransactions;

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleTransactionsViewModel"/> class.
        /// </summary>
        public VehicleTransactionsViewModel()
            : this(ServiceLocator.Current.GetInstance<IVehicleTransactionManager>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleTransactionsViewModel"/> class.
        /// </summary>
        /// <param name="vehicleTransactionManager">The vehicle transaction manager.</param>
        public VehicleTransactionsViewModel(IVehicleTransactionManager vehicleTransactionManager)
        {
            Contract.Requires<ArgumentNullException>(vehicleTransactionManager != null);

            _vehicleTransactionManager = vehicleTransactionManager;
        }

        /// <summary>
        /// Gets the accounts.
        /// </summary>
        public ObservableCollection<VehicleTransaction> VehicleTransactions
        {
            get
            {
                if (_vehicleTransactions == null)
                {
                    _vehicleTransactions = new ObservableCollection<VehicleTransaction>(_vehicleTransactionManager.List().Select(x => x.Map<Business.Entities.VehicleTransaction, VehicleTransaction>()));
                }

                return _vehicleTransactions;
            }

            private set
            {
                if (value != _vehicleTransactions)
                {
                    _vehicleTransactions = value;

                    OnPropertyChanged("VehicleTransactions");
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected account.
        /// </summary>
        /// <value>
        /// The selected account.
        /// </value>
        public VehicleTransaction SelectedVehicleTransaction
        {
            get
            {
                return _selectedVehicleTransaction;
            }

            set
            {
                if (_selectedVehicleTransaction != value)
                {
                    _selectedVehicleTransaction = value;

                    OnPropertyChanged("SelectedVehicleTransaction");
                    SelectedVehicleTransactionViewModel = new VehicleTransactionViewModel(value);
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected vehicle transaction view model.
        /// </summary>
        /// <value>
        /// The selected vehicle transaction view model.
        /// </value>
        public VehicleTransactionViewModel SelectedVehicleTransactionViewModel { get; set; }

        /// <summary>
        /// Refreshes the list.
        /// </summary>
        /// <param name="obj">The object</param>
        public void RefreshList(object obj)
        {
            VehicleTransactions = new ObservableCollection<VehicleTransaction>(_vehicleTransactionManager.List().Select(x => x.Map<Business.Entities.VehicleTransaction, VehicleTransaction>()));
        }

        /// <summary>
        /// Edits the transaction.
        /// </summary>
        /// <param name="obj">The object</param>
        public void EditVehicleTransaction(object obj)
        {
            var viewModel = new VehicleTransactionEditorViewModel(SelectedVehicleTransaction);
            var transactionEditorDocument = new VehicleTransactionEditorDocument(viewModel);

            DocumentManager.Instance.ActiveDocument = transactionEditorDocument;
        }

        /// <summary>
        /// Deletes the transaction.
        /// </summary>
        /// <param name="obj">The object</param>
        public void DeleteVehicleTransaction(object obj)
        {
            if (ServiceLocator.Current.GetInstance<IWindowDialogService>().ShowMessage(
                   "Are you sure you want to delete selected account?",
                   "Hissab",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question)
                   == DialogResult.Yes)
            {
                _vehicleTransactionManager.Delete(SelectedVehicleTransaction.Map<VehicleTransaction, Business.Entities.VehicleTransaction>());
                VehicleTransactions.Remove(SelectedVehicleTransaction);
            }
        }

        /// <summary>
        /// Determines whether this instance [can edit vehicle transaction] the specified obj.
        /// </summary>
        /// <param name="obj">The object</param>
        /// <returns>
        /// True if this instance can edit vehicle transaction the specified obj; otherwise, false.
        /// </returns>
        public bool CanEditVehicleTransaction(object obj)
        {
            return SelectedVehicleTransaction != null;
        }

        /// <summary>
        /// Determines whether this instance [can delete vehicle transaction] the specified obj.
        /// </summary>
        /// <param name="obj">The object </param>
        /// <returns>
        ///   <c>true</c> if this instance [can delete vehicle transaction] the specified obj; otherwise, <c>false</c>.
        /// </returns>
        public bool CanDeleteVehicleTransaction(object obj)
        {
            return SelectedVehicleTransaction != null;
        }
    }
}